package cn.com.oceansoft.nos.common.util.web;

import cn.com.oceansoft.business.base.entity.Pager;
import cn.com.oceansoft.business.base.entity.Query;
import com.github.pagehelper.Page;

/**
 * <strong>PageBreakUtil</strong><br>
 * 页面分页工具类<br>
 * <strong>Create on : 2012-1-5<br></strong>
 * <p/>
 * <strong>Copyright (C) Ecointel Software Co.,Ltd.<br></strong>
 * <p/>
 *
 * @author peng.shi peng.shi@ecointel.com.cn<br>
 * @version <strong>Ecointel v1.0.0</strong><br>
 */
public class PageUtil {

    private int pageNo;
    private int pageSize;
    private int offset;
    private int pages;
    private int rows;

    /**
     * 构造函数
     *
     * @param rows     总行数
     * @param pageNo   当前页号
     * @param pageSize 页面大小
     */
    public PageUtil(int rows, int pageNo, int pageSize) {
        if (pageSize <= 0) {
            this.pageSize = 10;
        } else {
            this.pageSize = pageSize;
        }

        if (pageNo <= 0) {
            pageNo = 1;
            this.pageNo = 1;
        } else {
            this.pageNo = pageNo;
        }

        if (rows <= 0) {
            this.pageNo = 1;
            this.pages = 1;
            this.offset = 0;
            this.rows = 0;
            return;
        }

        this.rows = rows;
        int tmp = rows % this.pageSize;

        if (tmp == 0) {
            this.pages = rows / this.pageSize;
        } else {
            this.pages = rows / this.pageSize + 1;
        }
        if (pageNo > pages) {
            this.pageNo = pages;
        }
        this.offset = (this.pageNo - 1) * pageSize;
    }

    /**
     * 返回当前页
     *
     * @return Returns the pageNo.
     */
    public int getPageNo() {
        return pageNo;
    }

    /**
     * 获取当前
     *
     * @return Returns the pageSize.
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * 获取数据库第几项偏移量
     *
     * @return Returns the offset.
     */
    public int getOffset() {
        return offset;
    }

    /**
     * 获取总页数
     *
     * @return Returns the pages.
     */
    public int getPages() {
        return pages;
    }

    /**
     * 获取行数
     *
     * @return Returns the rows.
     */
    public int getRows() {
        return rows;
    }

    /**
     * 返回页面VO对象
     *
     * @return
     */
    public PageInfo getPageInfo() {
        PageInfo info = new PageInfo();
        info.setPages(this.getPages());
        info.setRows(this.getRows());
        info.setPageNo(this.getPageNo());
        info.setPageSize(this.getPageSize());
        return info;
    }

    public static Pager page2pager(Page page, Query query) {
        if (null == page)
            return null;

        Pager pager = new Pager(query, page.getResult(), Integer.parseInt(page.getTotal() + ""));
        return pager;
    }
}
